package com.yinp.proapp.module.wanandroid.activity;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.yinp.proapp.R;
import com.yinp.proapp.base.activity.PresenterWanAndroidActivity;
import com.yinp.proapp.databinding.ActivityWandroidBinding;
import com.yinp.proapp.module.wanandroid.dialog.DialogShow;
import com.yinp.proapp.module.wanandroid.fragment.WanHomeFragment;
import com.yinp.proapp.module.wanandroid.fragment.WanNavigationFragment;
import com.yinp.proapp.module.wanandroid.fragment.WanOfficialAccountFragment;
import com.yinp.proapp.module.wanandroid.fragment.WanProjectClassifyFragment;
import com.yinp.proapp.module.wanandroid.fragment.WanProjectFragment;
import com.yinp.proapp.module.wanandroid.fragment.WanQuestionAnswerFragment;
import com.yinp.proapp.module.wanandroid.fragment.WanSquareFragment;
import com.yinp.proapp.module.wanandroid.fragment.WanSystemFragment;
import com.yinp.proapp.utils.AppUtils;
import com.yinp.proapp.utils.StatusBarUtil;
import com.yinp.proapp.view.viewpager2.SimplePagerTitlePictureView;
import com.yinp.proapp.view.viewpager2.ViewPager2Utils;

import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 玩Android总页面
 */
public class WandroidActivity extends PresenterWanAndroidActivity<ActivityWandroidBinding> {

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(this));
        bd.header.headerCenterTitle.setText("玩Android");
        bd.header.headerEnd.setImageResource(R.mipmap.common_software);
        initClick(bd.header.headerBackImg, bd.header.headerEnd, bd.ivMe);
        initIndicator();
    }

    /**
     * 不使用集合来管理的好处是，不会内存泄漏
     */
    private static class WanFragmentStateAdapter extends FragmentStateAdapter {
        public WanFragmentStateAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
            super(fragmentManager, lifecycle);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            Fragment fragment;
            switch (position) {
                case 0:
                    fragment = WanHomeFragment.getInstance();
                    break;
                case 1:
                    fragment = WanSquareFragment.getInstance();
                    break;
                case 2:
                    fragment = WanNavigationFragment.getInstance();
                    break;
                case 3:
                    fragment = WanQuestionAnswerFragment.getInstance();
                    break;
                case 4:
                    fragment = WanSystemFragment.getInstance();
                    break;
                case 5:
                    fragment = WanProjectFragment.getInstance();
                    break;
                case 6:
                    fragment = WanOfficialAccountFragment.getInstance();
                    break;
                case 7:
                    fragment = WanProjectClassifyFragment.getInstance();
                    break;
                default:
                    fragment = new Fragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getItemCount() {
            return 8;
        }
    }

    private void initIndicator() {
        bd.materialViewPager.setAdapter(new WanFragmentStateAdapter(getSupportFragmentManager(), getLifecycle()));
        bd.materialViewPager.setOffscreenPageLimit(1);
        List<String> titleList = new ArrayList<>(Arrays.asList("首页", "广场", "导航", "问答", "体系", "项目", "公众号", "项目分类"));
        bd.materialIndicator.setBackgroundColor(Color.WHITE);
        CommonNavigator commonNavigator7 = new CommonNavigator(mContext);
        commonNavigator7.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitlePictureView simplePagerTitleView = new SimplePagerTitlePictureView(context);
                simplePagerTitleView.setText(titleList.get(index));
                simplePagerTitleView.setNormalColor(ContextCompat.getColor(context, R.color.b8b8b8));
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setSelectedColor(ContextCompat.getColor(context, R.color.ff4d4d));
                simplePagerTitleView.setOnClickListener(v -> bd.materialViewPager.setCurrentItem(index, false));
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(UIUtil.dip2px(context, 0));
                indicator.setLineWidth(UIUtil.dip2px(context, 56));
                indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(ContextCompat.getColor(context, R.color.ff4d4d));
                return indicator;
            }
        });
        bd.materialIndicator.setNavigator(commonNavigator7);
        ViewPager2Utils.bind(bd.materialIndicator, bd.materialViewPager);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == bd.header.headerBackImg) {
            finish();
        } else if (v == bd.ivMe) {
            if (AppUtils.isLogin(mContext)) {
                goToActivity(WanMeActivity.class);
            } else {
                DialogShow.setLoginDialog(mContext, presenter, true, WanMeActivity.class, getSupportFragmentManager());
            }
        }
    }

    @Override
    protected void onDestroy() {
        ViewPager2Utils.unBind(bd.materialViewPager);
        super.onDestroy();
    }
}